package com.zhongke.common.widget

import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.TextView
import com.airbnb.lottie.LottieAnimationView
import com.zhongke.common.R

/**
 *@Date: 2022/4/8
 *@Time: 16:40
 *@Author:cxc
 *@Description:带lottie实现的底部导航栏按钮
 */
class ZKLottieTabButton : LinearLayout {

    private lateinit var lottieView: LottieAnimationView
    private lateinit var tvName: TextView

    private var tabCheck = false

    private var unCheckTextColor: Int = 0
    private var checkTextColor: Int = 0
    //未选中的时候是否倒叙播放动画
    private var reverser = false

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView(context, attrs, defStyleAttr)
    }

    private fun initView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.ZKLottieTabButton)
        val view = LayoutInflater.from(context).inflate(R.layout.view_lottie_tab_button, this, true)
        lottieView = view.findViewById(R.id.lottie_img)
        tvName = view.findViewById(R.id.tv_name)

        ta.run {
            val tabName = ta.getString(R.styleable.ZKLottieTabButton_tabName)
            val lottieFileName = ta.getString(R.styleable.ZKLottieTabButton_lottieFileName)
            unCheckTextColor = ta.getColor(
                R.styleable.ZKLottieTabButton_unCheckTextColor,
                context.getColor(R.color.base_color_666666)
            )
            checkTextColor = ta.getColor(
                R.styleable.ZKLottieTabButton_checkTextColor,
                context.getColor(R.color.base_color_ABAFB3)
            )
            val lottieFolder = ta.getString(R.styleable.ZKLottieTabButton_lottieFolder)
            val textSize  =ta.getDimension(R.styleable.ZKLottieTabButton_tabTextSize,0f)
            val iconSize = ta.getDimension(R.styleable.ZKLottieTabButton_iconSize,0f)
            tabCheck = ta.getBoolean(R.styleable.ZKLottieTabButton_tabCheck,false)
            reverser = ta.getBoolean(R.styleable.ZKLottieTabButton_reverse,false)


            lottieView.apply {
                imageAssetsFolder = lottieFolder
                setAnimation(lottieFileName)
                val params1 = layoutParams
                params1.width = iconSize.toInt()
                params1.height = iconSize.toInt()
                layoutParams = params1

            }

            tvName.apply {
                setTextColor(unCheckTextColor)
                text = tabName
                setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize)
            }

            recycle()
        }
        //如果默认选中，跳到选中状态
        if (tabCheck){
            doCheck()
        }
    }

    fun setChecked(isCheck: Boolean) {
        tabCheck = isCheck
        if (tabCheck){
            doCheck()
        }else{
            doUnCheck()
        }
    }

    /**
     * 没找到倒序播放的方法，用reverseAnimationSpeed()实现倒序，为了避免
     * 动画播放顺序出错，设立这个标志表示是否从未选中状态回到选中状态。
     */
    private  var stateChangeFromUncheck =false

    private fun doCheck() {
        if (reverser && stateChangeFromUncheck){
            lottieView.reverseAnimationSpeed()
        }
        lottieView.playAnimation()
        tvName.setTextColor(checkTextColor)
    }

    private fun doUnCheck() {
        if (reverser) {
            lottieView.reverseAnimationSpeed()
            lottieView.playAnimation()
            stateChangeFromUncheck = true
        }else{
            if (lottieView.isAnimating){
                lottieView.pauseAnimation()
            }
            lottieView.progress = 0f
        }
        tvName.setTextColor(unCheckTextColor)
    }


}
